<template>
  <div class="zh-class-members">
    <nav-header>
      <template slot="menuBefore">
        <div class="th-title-icon">
          <el-button
            @click="fieldsTips = true"
            icon="el-icon-warning"
            circle
            title="提示"
            size="mini"
          ></el-button>
        </div>
      </template>
    </nav-header>

    <transition name="fade" mode="out-in" v-if="activePanel === 'class-members'">
      <div class="zh-table" v-loading="listLoading.status" :element-loading-text="listLoading.txt">
        <div class="zh-table-open clear">
          <div class="zh-open-left fl">
            <div class="open-width" v-if="$store.state.checkLevel">
              <el-select v-model="section_id" filterable @change="getOrgLevel3" placeholder="请选择">
                <el-option
                  v-for="item in orgLevel2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
            <div class="open-width" v-if="$store.state.checkLevel">
              <el-select v-model="grade_id" filterable placeholder="请选择">
                <el-option
                  v-for="item in orgLevel3"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </div>
            <div class="open-width" style="width: 220px">
              <el-input
                v-model="keyword"
                placeholder="请输入班级名，班主任名搜索"
                @keyup.enter.native="searchFunc"
              ></el-input>
            </div>
            <el-button type="primary" @click="searchFunc">搜索</el-button>
            <el-button type="primary" @click="reset">重置</el-button>
          </div>
          <div class="zh-open-right fr">
            <el-button type="primary" v-if="!$store.state.checkLevel " @click="addClassBox">添加班级</el-button> 
            <el-button type="primary" @click="isTerm = true">学期管理</el-button>
            <!-- <el-button type="primary" @click="manualUpClass">手动升年级</el-button>
            <el-button type="primary" v-show="cate_type === 1" @click="upToGrade">自动升年级</el-button> -->
          </div>
        </div>

        <div class="zh-table-contents">
          <el-table :data="classData" border max-height="600" style="width: 100%">
            <el-table-column
              type="index"
              :index="typeIndex"
              label="序号"
              align="center"
              show-overflow-tooltip
              width="50"
            ></el-table-column>
            <el-table-column prop label="班级" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span>{{scope.row.grade_name + '-' + scope.row.name}}</span>
              </template>
            </el-table-column>
            <el-table-column prop="term" label="学年学期" align="center" show-overflow-tooltip></el-table-column>
            <!-- <el-table-column
					      prop="equal_name"
					      label="班级类别"
					      align="center"
					      show-overflow-tooltip
					      >
            </el-table-column>-->
            <!-- <el-table-column
					      prop="grade_name"
					      label="年级"
					      align="center"
					      show-overflow-tooltip
					      >
            </el-table-column>-->
            <!-- <el-table-column
					      prop="student_num"
					      label="学生人数"
					      align="center"
					      show-overflow-tooltip
					      >
            </el-table-column>-->
            <el-table-column prop="teacher_name" label="班主任" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="create_time" label="创建时间" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="password" label="班级邀请码" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop label="状态" align="center" show-overflow-tooltip>
              <template slot-scope="scope">
                <span v-if="parseInt(scope.row.status) === 1">启用中</span>
                <span v-if="parseInt(scope.row.status) === 2">禁用中</span>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" align="center" width="130">
              <template slot-scope="scope" v-if="scope.row.status === 1">
                <el-button @click="editClass(scope.row)" type="text">编辑</el-button>
                <el-button @click="jumpEdit(scope.row.id, scope.row.grade_name)" type="text">班级管理</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="table-pagination" v-show="total_number > 10">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="classParam.page"
              :page-sizes="[10, 20, 30, 40, 50, 100]"
              :page-size="classParam.size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total_number"
            ></el-pagination>
            <div class="pagination-button">
              <el-button
                @click="geClassList"
                type="primary"
                icon="el-icon-refresh"
                circle
                title="刷新数据"
                size="mini"
              ></el-button>
            </div>
          </div>
        </div>
        <!-- 添加班级 -->
        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :before-close="cancelAddClass"
          title="添加班级"
          :visible.sync="addClass"
          class="classMembers"
          width="450px"
        >
          <el-form
            :model="addForm"
            ref="addForm"
            :rules="rules"
            status-icon
            label-position="left"
            label-width="80px"
            v-loading="boxLoading.status"
            :element-loading-text="boxLoading.txt"
          >
            <el-form-item label="学段" prop="teacherParam.org_id">
              <el-select
                v-model="addForm.teacherParam.org_id"
                filterable
                @change="getOrgLevelBox3('add')"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in orgLevelBox2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="年级" prop="pid">
              <el-select
                v-model="addForm.pid"
                placeholder="请选择"
                filterable
              ><!--@change="getAddClassList(addForm.pid)"-->
                <el-option
                  v-for="item in orgLevelBox3"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="班级" prop="name">
              <el-input type="text" placeholder="请输入班级名称" v-model.trim="addForm.name" maxlength="10"></el-input> 
              <!-- <el-select v-model="addForm.name" filterable placeholder="请选择添加的班级">
                <el-option
                  v-for="(item, index) in addClassList"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select> -->
            </el-form-item>
            <el-form-item label="班主任" prop="base_id">
              <el-select v-model="addForm.base_id" filterable placeholder="请选择班主任">
                <el-option
                  v-for="item in headTeacher"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="4"
                :maxlength="50"
                placeholder="请输入备注内容"
                v-model="addForm.remark"
              ></el-input>
              <span class="textarea-limit">{{addForm.remark.length}}/50</span>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="addForm.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="2">关闭</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancelAddClass">取消</el-button>
            <el-button type="primary" @click="submitAddClasses">确定</el-button>
          </div>
        </el-dialog>

        <el-dialog
          :close-on-click-modal="false"
          :close-on-press-escape="false"
          :before-close="cancelEditClass"
          title="编辑班级"
          :visible.sync="editClassDialog"
          class="classMembers"
          width="450px"
        >
          <el-form
            :model="editForm"
            ref="editForm"
            :rules="rules"
            status-icon
            label-position="left"
            label-width="80px"
            v-loading="boxLoading.status"
            :element-loading-text="boxLoading.txt"
          >
            <el-form-item label="学段" prop="teacherParam.org_id">
              <el-select
                v-model="editForm.teacherParam.org_id"
                filterable
                @change="getOrgLevelBox3('edit')"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in orgLevelBox2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="年级" prop="pid">
              <el-select
                v-model="editForm.pid"
                placeholder="请选择"
                filterable
                
              ><!-- @change="getAddClassList(editForm.pid, 'edit1')" -->
                <el-option
                  v-for="item in orgLevelBox3"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="班级" prop="name">
              <el-input type="text" placeholder="请输入班级名称" v-model.trim="editForm.name" maxlength="10"></el-input> 
              <!-- <el-select v-model="editForm.name" filterable placeholder="请选择添加的班级">
                <el-option
                  v-for="(item, index) in addClassList"
                  :key="index"
                  :label="item"
                  :value="item"
                ></el-option>
              </el-select> -->
            </el-form-item>
            <el-form-item label="班主任" prop="base_id">
              <el-select v-model="editForm.base_id" filterable placeholder="请选择班主任">
                <el-option
                  v-for="item in headTeacher"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input
                type="textarea"
                :rows="4"
                :maxlength="50"
                placeholder="请输入备注内容"
                v-model="editForm.remark"
              ></el-input>
              <span class="textarea-limit">{{editForm.remark.length}}/50</span>
            </el-form-item>
            <el-form-item label="状态">
              <el-radio-group v-model="editForm.status">
                <el-radio label="1">启用</el-radio>
                <el-radio label="2">关闭</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="cancelEditClass">取消</el-button>
            <el-button type="primary" @click="submitEditClassess">确定</el-button>
          </div>
        </el-dialog>
      </div>
    </transition>

    <transition name="fade" mode="out-in" v-if="activePanel === 'graduation'">
      <zh-class-graduation />
    </transition>
    <el-dialog
      title="提示"
      :visible.sync="fieldsTips"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="580px"
    >
      <ul style="padding: 20px 30px">
        <li style="list-style-type: disc;">若想 删除 班级 请到组织架构中进行操作</li>
        <li style="list-style-type: disc;">班级邀请码在新建班级的时候自动创建，可用邀请学生或者教师加入班级</li>
        <li style="list-style-type: disc;">开启了自动升年级之后到第二学年的秋季（即第二学期）自动升年级</li>
        <li style="list-style-type: disc;">每升一次年级，班级邀请码更新一次</li>
      </ul>
      <div slot="footer" class="dialog-footer"></div>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="isTerm"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
      class="dialog-term"
    >
      <span>即将跳转至【学期管理】页，确认跳转吗？</span>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isTerm = false">取消</el-button>
        <el-button type="primary" @click="toTermManagement">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="升年级设置"
      :visible.sync="isUpGrade"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
      class="dialog-up-grade"
    >
      <el-form ref="gradeForm" :model="gradeForm" label-width="100px" label-position="left">
        <el-form-item label="自动升年级：">
          <el-radio-group v-model="gradeForm.grade">
            <el-radio label="1">启用</el-radio>
            <el-radio label="2">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span style="color: #FF0000">须知：</span>
      <p>启用自动升年级后，到下一学年的秋季开始时间自动升年级，全部班级自动往上升一个年级。</p>
      <p>毕业年级（六年级、初三、高三）的状态升级为毕业生</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isUpGrade = false">取消</el-button>
        <el-button type="primary" @click="upGrade">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="调班"
      :visible.sync="isChangeClass"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="500px"
    >
      <el-form ref="classForm" :model="classForm" label-width="100px" label-position="right">
        <el-form-item label="调整角色：">
          <el-radio-group v-model="classForm.role">
            <el-radio label="所有"></el-radio>
            <el-radio label="仅学生"></el-radio>
            <el-radio label="仅教师"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="新班级：">
          <el-select v-model="classForm.class" placeholder="请选择新班级" style="width: 245px">
            <el-option
              v-for="(item, index) in classResult"
              :key="index"
              :label="item"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isChangeClass = false">取消</el-button>
        <el-button type="primary" @click="changeClass">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="isSure"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
      class="dialog-sure"
    >
      <p>要将班级从3班更替为5班吗？</p>
      <p style="color: #777; font-size: 13px;">原来的5班数据将变为3班</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isSure = false">取消</el-button>
        <el-button type="primary" @click="saveSure">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="isTermSetting"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
    >
      <p>开启自动升年级需先填写学期日期区间，需要跳转到【学期管理】吗？</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isTermSetting = false">取消</el-button>
        <el-button type="primary" @click="toTermManagement">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="声明"
      :visible.sync="isDeclaration"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
    >
      <p>进入下一个学期后，所有班级会自动升年级且数据不可逆，是否继续自动升年级操作？</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isDeclaration = false">取消</el-button>
        <el-button type="primary" @click="showAdmin">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="声明"
      :visible.sync="isautomatic"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="400px"
    >
      <p>开启后，当进入下个学年时，所有班级会自动向上升个年级，确认是否开启自动升年级操作？</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isautomatic = false">取消</el-button>
        <el-button type="primary" @click="showAdmin">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="输入密码"
      :visible.sync="isAdmin"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="clearPass"
      width="400px"
    >
      <!-- <span style="display: block;margin-bottom:22px">请输入超级管理员密码，并确认本次操作。</span> -->
      <el-input type="password"
          v-model="passForm.pass" 
          placeholder="请输入超级管理员密码，并确认本次操作。"
          @keyup.enter.native="gradeSet"
          autocomplete="off">
      </el-input>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="clearPass">取消</el-button>
        <el-button type="primary" @click="gradeSet">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      cate_type: null,
      listLoading: {
        status: false,
        txt: "获取数据...",
      },
      boxLoading: {
        status: false,
        txt: "获取数据...",
      },
      activePanel: "class-members",
      orgLevel2: [{ id: 0, name: "全部学段" }], //列表学段下拉
      orgLevel3: [{ id: 0, name: "全部年级" }], //列表年级下拉
      orgLevelBox2: [], //弹框学段下拉
      orgLevelBox3: [], //弹框年级下拉
      classData: [], //班级列表
      addClassList: [] /*存放未创建班级列表 */,
      headTeacher: [], //班主任
      classParam: {
        //列表参数
        status: [1, 2],
        keyword: "",
        page: 1,
        size: 10,
        section_id: 0,
        grade_id: 0,
        data_type: 2,
      },
      section_id: 0,
      grade_id: 0,
      keyword: "",
      total_number: 0, //列表总数量
      addClass: false, // 新增弹框
      editClassDialog: false, // 编辑弹框
      addForm: {
        //弹框表单
        id: null,
        pid: "",
        name: "",
        remark: "",
        level: 4,
        base_id: "",
        status: "1",
        teacherParam: {
          //班主任参数
          org_id: 0,
          role_id: 6,
          status: 1,
        },
      },
      editForm: {
        id: null,
        pid: "",
        name: "",
        remark: "",
        level: 4,
        base_id: "",
        status: "1",
        teacherParam: {
          //班主任参数
          org_id: 0,
          role_id: 6,
          status: 1,
        },
      },
      rules: {
        name: [
          { required: true, message: "班级名称不能为空", trigger: "blur" },
          { min: 2, max: 20, message: "长度在 2 到 20 个字符" },
          { pattern: /^\S+$/, message: "不能包含空格" },
        ],
        pid: [
          { validator: this.checkChange, trigger: "change", required: true },
        ],
        remark: [
          // {min: 0,max: 50,message: '长度在 0 到 50 个字符'},
          { pattern: /^\S+$/, message: "不能包含空格" },
        ],
        base_id: [
          { validator: this.checkChange, trigger: "change", required: true },
        ],
        'teacherParam.org_id': [
          { validator: this.checkChange, trigger: "change", required: true },
        ],
      },
      orgLevelParam: {
        //获取学段,年纪参数
        app_code: sessionStorage.getItem("CheckSchoolAppCode"),
        level: 2,
        pid: 1,
        direction: "balance",
      },
      orgLevelParamBox: {
        //获取学段,年纪参数
        app_code: sessionStorage.getItem("CheckSchoolAppCode"),
        level: 2,
        pid: 1,
        direction: "balance",
      },
      boxEdit: false,
      orgNum: 1,
      fieldsTips: false,
      isTerm: false,
      isUpGrade: false,
      gradeForm: {
        grade: "2",
        section: "2",
      },
      isChangeClass: false,
      classForm: {
        id: null,
        role: "所有",
        class: null,
      },
      classResult: [],
      isSure: false,
      timer: null,
      isTermSetting: false,
      isDeclaration: false,
      isAdmin: false,
      passForm: {
        pass: '',
      },
      rules2: {
        pass: [
          {
            required: true,
            message: "请输入当前密码进行安全认证",
            trigger: "blur",
          },
        ],
      },
      classUpData: null,
      isManual: false,
      isautomatic: false,
      isSetTerm: false,
    };
  },
  created() {
    this.getSchoolInfo()
    this.toggleView();
    this.geClassList();
    this.getOrgLevel2();
    this.classUp();
    this.getSetTerm();
  },
  mounted() {
    for (let i = 1; i <= 50; i++) {
      this.classResult.push(i + "班");
    }
  },
  watch: {
    $route() {
      this.toggleView();
    },
    addClass() {
      if (this.addClass === false) {
        this.$refs.addForm.clearValidate();
        this.$refs.addForm.resetFields();
      }
    },
    editClassDialog() {
      if (this.editClassDialog === false) {
        this.$refs.editForm.clearValidate();
        this.$refs.editForm.resetFields();
      }
    },
  },
  methods: {
    getSchoolInfo() {
      let params = {
        app_code: sessionStorage.getItem("CheckSchoolAppCode")
      }
      let success = res => {
        console.log(res.data.cate_type)
        this.cate_type = res.data.cate_type
      }
      this.$ajax.get('api/newlevel/platform', {params, func: {success}})
    },
    /**
     * 弹窗清空密码
     */
    showAdmin() {
      this.isAdmin = true;

      setTimeout(() => {
        this.isautomatic = false;
        this.isDeclaration = false;
      }, 500);
    },
    /**
     * 清空密码
     */
    clearPass() {
      this.isAdmin = false;
      this.passForm.pass = "";
    },
    /**
     * 是否已经设置学期管理
     */
    getSetTerm() {
      let success = (data) => {
        if (data.data === "YES") {
          this.isSetTerm = true;
        } else {
          this.isSetTerm = false;
        }
      };

      this.$ajax.get("api/term/get_can_class_up", {
        params: {},
        func: { success: success },
      });
    },
    manualUpClass() {
      this.isDeclaration = true;
      this.isManual = true;
    },
    gradeSet() {
      if(this.passForm.pass == '') {
          this.$message.warning('请输入超级管理员密码')
          return
      }
      const success = (res) => {
        this.isAdmin = false;
        this.isDeclaration = false;
        this.isUpGrade = false;
        this.isautomatic = false;
        this.isManual = false;
        this.$message.success(res.msg);
        this.geClassList();
        this.getCurrentTerm();
        this.classUp();
      };

      const checkSuccess = (res) => {
        if (this.isManual === true) {
          let params = { is_delete_class_up: 0 };
          this.$ajax.put("api/org/manual_class_up", params, {
            func: { success: success },
          });
        } else {
          let params = { is_grate_up: this.gradeForm.grade };
          this.$ajax.put("api/org/org_class_up", params, {
            func: { success: success },
          });
        }
      };

      const checkParams = {
        id: this.$g.user_id,
        password: this.passForm.pass,
      };

      this.$ajax.get("api/user/check_user_by_id", {
        params: checkParams,
        func: { success: checkSuccess },
      });
    },
    upToGrade() {
      this.isManual = false;
      // 是否已经设置学期
      if (this.isSetTerm) {
        this.isUpGrade = true;
        if (this.classUpData.automatic === true) {
          this.gradeForm.grade = "1";
        }
      } else {
        this.isTermSetting = true;
      }
    },
    toTermManagement() {
      this.isTerm = false;
      this.isTermSetting = false;
      this.$router.push("/affairs/semester/term-list");
    },
    upGrade() {
      if (this.gradeForm.grade === "1") {
        this.isautomatic = true;
      } else {
        this.showAdmin();
      }
    },
    showChangeClass() {
      this.classForm.id = this.addForm.id;
      this.isChangeClass = true;
    },
    changeClass() {
      const success = (res) => {
        this.isChangeClass = false;
        this.$message.success(res.msg);
      };

      let temp = "all";
      if (this.classForm.role === "所有") {
        temp = "all";
      } else if (this.classForm.role === "仅学生") {
        temp = "student";
      } else if (this.classForm.role === "仅教师") {
        temp = "teacher";
      }

      let params = {
        class_id1: this.classForm.id,
        class2_name: this.classForm.class,
        type: temp,
      };

      this.$ajax.put("api/org/class_exchange", params, {
        func: { success: success },
      });
    },
    saveSure() {},
    searchFunc() {
      this.classParam.keyword = this.keyword;
      this.classParam.section_id = this.section_id;
      this.classParam.grade_id = this.grade_id;
      this.classParam.page = 1;
      this.geClassList();
    },
    reset() {
      this.keyword = "";
      this.section_id = 0;
      this.grade_id = 0;
      this.classParam = {
        status: [1],
        keyword: "",
        page: 1,
        size: 10,
        section_id: 0,
        grade_id: 0,
        data_type: 2,
        school_id: this.$store.getters.userInfo.platform_id,
      };
      this.geClassList();
    },
    changePanel(panel) {
      this.activePanel = panel;
    },
    //验证
    validateName(rule, value, callback) {
      if (value === "") {
        return callback(new Error("请输入班级"));
      } else if (!validateName(value)) {
        return callback(new Error("请不要输入空格字符"));
      } else {
        callback();
      }
    },
    validateContent(rule, value, callback) {
      if (value === "") {
        return callback(new Error("请输入备注内容"));
      } else if (!validateName(value)) {
        return callback(new Error("请不要输入空格字符"));
      } else {
        callback();
      }
    },
    checkChange(rule, value, callback) {
      if (!isEmpty(value)) {
        return callback(new Error("请选择选项"));
      } else {
        callback();
      }
    },
    jumpEdit(id, grade_name) {
      this.$router.push("/user/class/class-management/" + id);
    },
    // 序号
    typeIndex(index) {
      return index + (this.classParam.page - 1) * this.classParam.size + 1;
    },
    //获取班级列表
    geClassList() {
      if (!this.$store.state.checkLevel) {
        this.classParam.school_id = this.$store.getters.userInfo.platform_id;
      }
      this.listLoading.status = true;
      let success = (data) => {
        this.listLoading.status = false;
        this.classData = data.data.data;
        this.total_number = data.data.data_sum;
        // console.log(data.data.data);
        this.getCurrentTerm();
      };
      this.classParam.org_id = parseInt(this.classParam.grade_id) > 0 ? this.classParam.grade_id : this.classParam.section_id;

      this.$ajax.get("api/org/classes", {
        params: this.classParam,
        func: { success: success },
      });
    },
    // 获取当前所在学期
    getCurrentTerm() {
      const success = (res) => {
        let temp = deep_clone(this.classData);
        this.classData = [];
        this.classData = temp;
        this.classData.forEach((item) => {
          item.term = res.data;
        });
        this.$forceUpdate(); //进行强制渲染
      };

      this.$ajax.get("api/term/get_now_term", {
        params: {},
        func: { success: success },
      });
    },
    // 获取平台是否设置了自动升级
    classUp() {
      const success = (res) => {
        this.classUpData = res.data;
      };
      this.$ajax.get("api/org/org_class_up", {
        params: {},
        func: { success: success },
      });
    },
    // 获取可添加的班级
    // getAddClassList(id, val) {
    //   const success = (res) => {
    //     console.log(res, 121);
    //     this.addClassList = res.data;
    //     this.addForm.name = null;
    //     if (val == "edit1") {
    //       this.editForm.name = null;
    //     }
    //   };
    //   let params = {
    //     id: id,
    //     school_id: this.$store.getters.userInfo.platform_id,
    //   };
    //   this.$ajax.get("api/org/get_name_by_school_grade", {
    //     params: params,
    //     func: { success: success },
    //   });
    // },
    /**
     * 保存班级
     */
    submitAddClasses() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          let success = (res) => {
            this.$message.success(res.msg);
            this.addClass = false;
            this.addClassList = [];
            this.geClassList();
          };

          this.addForm.remark = this.addForm.remark.replace(
            /^\s*(.*?)\s*$/,
            "$1"
          );

          this.$ajax.post("api/org/org", this.addForm, {
            func: { success: success },
          });
        } else {
          return false;
        }
      });
    },
    /**
     * 保存编辑班级
     */
    submitEditClassess() {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          let success = (res) => {
            this.$message.success(res.msg);
            this.editClassDialog = false;
            this.boxEdit = false;
            this.addClassList = [];
            this.geClassList();
          };

          this.editForm.remark = this.editForm.remark.replace(
            /^\s*(.*?)\s*$/,
            "$1"
          );

          this.$ajax.put("api/org/org", this.editForm, {
            func: { success: success },
          });
        } else {
          return false;
        }
      });
    },
    /**
             取消新增班级弹窗
             */
    cancelAddClass() {
      this.addClass = false;
      this.orgLevelBox3 = [];
      this.addClassList = [];
    },
    /**
             取消编辑班级弹窗
             */
    cancelEditClass() {
      this.editClassDialog = false;
      this.boxEdit = false;
      this.orgLevelBox3 = [];
      this.addClassList = [];
    },
    //获取学段
    getOrgLevel2() {
      let success = (data) => {
        this.orgLevel2 = deep_clone(data.data);
        this.orgLevel2.unshift({ id: 0, name: "全部学段" });
        this.orgLevelBox2 = deep_clone(data.data);
        // this.addForm.org_id = this.orgLevelBox2[0].id;
        this.addForm.teacherParam.org_id = this.orgLevelBox2[0].id;
        this.getOrgLevelBox3();
        this.getHeadTeacher(); // 获取教师
        this.getOrgLevel3(); // 获取年级
      };
      if (this.$store.state.checkLevel) {
        this.$ajax.get("api/newlevel/platform_org", {
          params: this.orgLevelParam,
          func: { success: success },
        });
      } else {
        this.$ajax.get("api/org/org", {
          params: this.orgLevelParam,
          func: { success: success },
        });
      }
    },
    //获取年级
    getOrgLevel3() {
      this.orgLevelParam.level = 3;
      this.orgLevelParam.pid = this.section_id;
      let success = (data) => {
        this.orgLevel3 = deep_clone(data.data);
        this.orgLevel3.unshift({ id: 0, name: "全部年级" });
        this.classParam.grade_id = this.orgLevel3[0].id;
      };
      if (this.$store.state.checkLevel) {
        this.$ajax.get("api/newlevel/platform_org", {
          params: this.orgLevelParam,
          func: { success: success },
        });
      } else {
        this.$ajax.get("api/org/org", {
          params: this.orgLevelParam,
          func: { success: success },
        });
      }
    },
    //获取弹框年级
    getOrgLevelBox3(name = null) {
      this.boxLoading.status = true;
      this.orgLevelParamBox.level = 3;
      this.orgLevelParamBox.pid = this.boxEdit ? this.editForm.teacherParam.org_id : this.addForm.teacherParam.org_id;
      this.getHeadTeacher();

      let success = (res) => {
        if (name === "add") {
          this.addForm.pid = null;
          this.addForm.name = null;
        }

        if (name === "edit") {
          if (this.orgNum === 1) {
            this.orgNum++;
          } else {
            this.editForm.pid = null;
            this.editForm.name = null;
          }
        }

        this.orgLevelBox3 = deep_clone(res.data);
        // console.log(this.orgLevelBox3, 9090)

        this.$nextTick(() => {
          this.$refs.addForm && this.$refs.addForm.clearValidate();
        });
      };
      if (this.$store.state.checkLevel) {
        this.$ajax.get("api/newlevel/platform_org", {
          params: this.orgLevelParamBox,
          func: { success: success },
        });
      } else {
        this.$ajax.get("api/org/org", {
          params: this.orgLevelParamBox,
          func: { success: success },
        });
      }
    },
    //获取班主任
    getHeadTeacher() {
      let success = (res) => {
        this.boxLoading.status = false;
        this.headTeacher = res.data;
      };
      let params = {
        role_id: this.addForm.teacherParam.role_id,
        status: this.addForm.teacherParam.status,
      };

      this.$ajax.get("api/user/user", {
        params: params,
        func: { success: success },
      });
    },
    // 新增班级
    addClassBox() {
      this.addClass = true;
      this.addForm.id = null;
      this.addForm.pid = "";
      this.addForm.name = "";
      this.addForm.status = "1";
      this.addForm.remark = "";
      this.addForm.base_id = "";
      this.addForm.teacherParam.org_id = '';
      this.getOrgLevelBox3("add");

      if (this.$refs.addForm) {
        this.$refs.addForm.clearValidate();
      }
    },
    /**
     * 编辑班级
     */
    editClass(obj) {
      console.log(obj, 77777)
      this.editClassDialog = true;
      this.boxEdit = true;
      this.orgNum = 1;
      this.editForm.teacherParam.org_id = obj.study_section_id;
      this.getOrgLevelBox3("edit");
      // this.getAddClassList(obj.pid);

      this.editForm.pid = obj.grade_id;
      this.editForm.id = obj.id;
      this.editForm.name = obj.name;
      this.editForm.status = obj.status.toString();
      this.editForm.remark = obj.remark;
      this.editForm.base_id = (parseInt(obj.base_id) === 0) ? '' : obj.base_id ;
    },
    //记录导航状态
    toggleView() {
      this.view = this.$route.name;
    },
    classMembers() {
      this.$router.push("/user/class/class-members/graduation");
    },
    classMembersList() {
      this.$router.push("/user/class/class-members");
    },
    handleSizeChange(val) {
      this.classParam.page = 1;
      this.classParam.size = val;
      this.geClassList();
    },
    handleCurrentChange(val) {
      this.classParam.page = val;
      this.geClassList();
    },
  },
};
</script>

<style lang="scss">
.zh-class-members {
  .th-title-icon {
    position: absolute;
    top: 0;
    right: 40px;

    i {
      color: #daa017;
      font-size: 16px;
    }
  }

  .textarea-limit {
    position: absolute;
    display: block;
    bottom: -30px;
    right: 0;
  }

  .zh-common-title-text {
    li {
      color: #000 !important;
    }
  }

  .edit-class {
    .el-select {
      width: 70%;
    }

    .edit-span {
      display: inline-block;
      text-align: center;
      width: 28%;
      cursor: pointer;

      i {
        color: #000;
        font-weight: bold;
        padding-right: 5px;
        font-size: 15px;
      }
    }
  }

  .dialog-term {
    .el-dialog__body {
      margin: 20px 0 30px;
      text-align: center;
    }
  }

  .dialog-sure .el-dialog__body {
    text-align: center;
  }

  .dialog-up-grade {
    .el-form-item--small.el-form-item {
      margin-bottom: 0;
    }

    p {
      color: #777;
      font-size: 13px;
    }
  }

  .zh-table {
    .zh-table-open {
      .open-width {
        width: 120px;
      }
    }
  }
  .el-table__body-wrapper table tbody {
    td:nth-child(10) {
      padding: 0;
    }
  }
  .classMembers {
    .editClass-disabled {
      > div {
        float: left;
        width: 220px;
      }
      > button {
        margin-left: 10px;
      }
    }
  }
}
</style>